﻿using QuanLyKho.DAL;
using QuanLyKho.DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyKho.GUI
{
    public partial class XuatForm : Form
    {
        public XuatForm()
        {
            InitializeComponent();
        }

        private void XuatForm_Load(object sender, EventArgs e)
        {
            initControls();
            generateMaPhieu();
        }

        private void generateMaPhieu()
        {
            int soLuongPhieu = new PhieuXuatDAO().getListPhieuNhap().Count;
            string maPhieu = "PX" + soLuongPhieu;
            textBoxMaPhieu.Text = maPhieu;
        }

        private void initControls()
        {
            reloadTenKhachHangComboBox();
            reloadMaHangComboBox();
        }

        private void reloadMaHangComboBox()
        {
            HangHoaDAO hangHoaDAO = new HangHoaDAO();
            ArrayList listHangHoa = hangHoaDAO.GetListHang();
            if (listHangHoa.Count == 0)
            {
                HangHoaDTO emptyHangHoa = new HangHoaDTO();
                emptyHangHoa.MaHang = "";
                emptyHangHoa.TenHang = "";
                listHangHoa.Add(emptyHangHoa);
            }
            cmbMaHang.DisplayMember = "MaHang";
            cmbMaHang.ValueMember = "TenHang";
            cmbMaHang.DataSource = listHangHoa;
        }

        private void btnThemKhachHang_Click(object sender, EventArgs e)
        {
            ThemMoiKhachHangForm themMoiKhacHangFrm = new ThemMoiKhachHangForm();
            themMoiKhacHangFrm.ShowDialog();
            reloadTenKhachHangComboBox();
        }

        private void reloadTenKhachHangComboBox()
        {
            KhachHangDAO khachHangDAO = new KhachHangDAO();
            ArrayList listKhachHang = khachHangDAO.GetListKhachHang();
            if (listKhachHang.Count == 0)
            {
                KhachHangDTO emptyKhachHang = new KhachHangDTO();
                emptyKhachHang.Ma = "";
                emptyKhachHang.Ten = "";
                listKhachHang.Add(emptyKhachHang);
            }
            cmbTenKhachHang.DisplayMember = "Ten";
            cmbTenKhachHang.ValueMember = "Ma";
            cmbTenKhachHang.DataSource = listKhachHang;
        }

        private void buttonThemHang_Click(object sender, EventArgs e)
        {
            if (textBoxMaPhieu.Text == "")
            {
                MessageBox.Show("Chưa nhập mã phiếu", "Lỗi");
                return;
            }

            if (cmbMaKhachHang.Text == "")
            {
                MessageBox.Show("Chưa chọn khách hàng", "Lỗi");
                return;
            }

            if (cmbMaHang.Text == "")
            {
                MessageBox.Show("Chưa chọn hàng", "Lỗi");
                return;
            }

            if (cmbNhaPhanPhoi.Text == "")
            {
                MessageBox.Show("Chưa chọn nhà phân phối", "Lỗi");
                return;
            }

            if (cmbMaKho.Text == "")
            {
                MessageBox.Show("Chưa chọn kho", "Lỗi");
                return;
            }

            PhieuXuatDAO phieuXuatDAO = new PhieuXuatDAO();
            PhieuXuatDTO phieuXuatDTO = new PhieuXuatDTO();

            phieuXuatDTO.MaPhieu = textBoxMaPhieu.Text;
            phieuXuatDTO.MaKH = cmbMaKhachHang.Text;
            phieuXuatDTO.MaHang = cmbMaHang.Text;
            phieuXuatDTO.MaNhaPhanPhoi = cmbNhaPhanPhoi.Text;
            phieuXuatDTO.NgayNhap = datePickerNgayXuat.Value;
            phieuXuatDTO.MaKho = cmbMaKho.Text;
            try
            {
                phieuXuatDTO.SoLuong = Int32.Parse(textBoxSoLuong.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Trường số lượng xuất không phải số", "Lỗi");
                return;
            }

            try
            {
                phieuXuatDAO.insert(phieuXuatDTO);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể thêm hàng", "Lỗi");
                return;
            }

            reloadDanhSachHangDataGridView();
        }

        private void reloadDanhSachHangDataGridView()
        {
            if (textBoxMaPhieu.Text != "" && cmbMaKhachHang.Text != "")
            {
                PhieuXuatDAO phieuXuatDAO = new PhieuXuatDAO();
                ArrayList listHangPhieuNhap = phieuXuatDAO.getListHangTrongPhieuNhapByMa(textBoxMaPhieu.Text, cmbMaKhachHang.Text);
                dataGridViewChiTietPhieuNhap.DataSource = listHangPhieuNhap;
            }
        }

        private void cmbTenKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            reloadMaKhachHangCombox();
        }

        private void reloadMaKhachHangCombox()
        {
            if (cmbTenKhachHang.Text != "")
            {
                KhachHangDAO khachHangDAO = new KhachHangDAO();
                ArrayList listKhachHang = khachHangDAO.GetListKhachHangByTen(cmbTenKhachHang.Text);
                if (listKhachHang.Count == 0)
                {
                    KhachHangDTO emptyKhachHang = new KhachHangDTO();
                    emptyKhachHang.Ma = "";
                    emptyKhachHang.Ten = "";
                    listKhachHang.Add(emptyKhachHang);
                }
                cmbMaKhachHang.DisplayMember = "Ma";
                cmbMaKhachHang.ValueMember = "Ten";
                cmbMaKhachHang.DataSource = listKhachHang;
            }
        }

        private void cmbMaHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxTenHang.Text = cmbMaHang.SelectedValue.ToString();
            reloadMaKhoCombox();
            reloadTongSoLuongTonTextBox();
        }

        private void reloadTongSoLuongTonTextBox()
        {
            int tongSoLuongTon = 0;
            if (cmbMaHang.Text != "" && cmbMaKho.Text != "" && cmbNhaPhanPhoi.Text != "")
            {
                PhieuNhapDAO phieuNhapDAO = new PhieuNhapDAO();
                int soLuongNhap = phieuNhapDAO.getTongSoLuongHangNhap(cmbMaHang.Text);

                PhieuXuatDAO phieuXuatDAO = new PhieuXuatDAO();
                int soLuongXuat = phieuXuatDAO.getTongSoLuongHangXuat(cmbMaHang.Text);

                tongSoLuongTon = soLuongNhap - soLuongXuat;
            }
            textBoxTongSoLuongTon.Text = tongSoLuongTon + " ";
        }

        private void reloadMaKhoCombox()
        {
            if (cmbMaHang.Text != "")
            { 
                KhoDAO khoDAO = new KhoDAO();
                ArrayList listKho = khoDAO.getListKhoByMaHang(cmbMaHang.Text);
                if (listKho.Count == 0)
                {
                    KhoDTO emptyKho = new KhoDTO();
                    emptyKho.MaKho = "";
                    emptyKho.TenKho = "";
                    listKho.Add(emptyKho);
                }
                cmbMaKho.DisplayMember = "MaKho";
                cmbMaKho.ValueMember = "TenKho";
                cmbMaKho.DataSource = listKho;
            }
        }

        private void cmbMaKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxTenKho.Text = cmbMaKho.SelectedValue.ToString();
            reloadMaNhaPhanPhoi();
        }

        private void reloadMaNhaPhanPhoi()
        {
            if (cmbMaHang.Text != "" && cmbMaKho.Text != "")
            { 
                NhaPhanPhoiDAO nhaPhanPhoiDAO = new NhaPhanPhoiDAO();
                ArrayList listNhaPhanPhoi = nhaPhanPhoiDAO.GetListNhaPhanPhoiByMaHangAndMaKho(cmbMaHang.Text, cmbMaKho.Text);
                if (listNhaPhanPhoi.Count == 0)
                {
                    NhaPhanPhoiDTO emptyNhaPhanPhoi = new NhaPhanPhoiDTO();
                    emptyNhaPhanPhoi.Ma = "";
                    emptyNhaPhanPhoi.Ten = "";
                    listNhaPhanPhoi.Add(emptyNhaPhanPhoi);
                }
                cmbNhaPhanPhoi.DisplayMember = "Ma";
                cmbNhaPhanPhoi.ValueMember = "Ten";
                cmbNhaPhanPhoi.DataSource = listNhaPhanPhoi;
            }
        }

        private void cmbMaKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbNhaPhanPhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxTenNhaPhanPhoi.Text = cmbNhaPhanPhoi.SelectedValue.ToString();
            reloadSoLuongTon();
        }

        private void reloadSoLuongTon()
        {
            int soLuongTon = 0;
            if (cmbMaHang.Text != "" && cmbMaKho.Text != "" && cmbNhaPhanPhoi.Text != "")
            {
                PhieuNhapDAO phieuNhapDAO = new PhieuNhapDAO();
                int soLuongNhap = phieuNhapDAO.getSoLuongHangNhap(cmbMaHang.Text, cmbMaKho.Text, cmbNhaPhanPhoi.Text);

                PhieuXuatDAO phieuXuatDAO = new PhieuXuatDAO();
                int soLuongXuat = phieuXuatDAO.getSoLuongHangXuat(cmbMaHang.Text, cmbMaKho.Text, cmbNhaPhanPhoi.Text);

                soLuongTon = soLuongNhap - soLuongXuat;
            }
            textBoxSoLuongTon.Text = soLuongTon + " ";
        }

    }
}
